package com.raccoonagents.tools.cascet2d.model.components;


import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import com.raccoonagents.tools.cascet2d.interfaces.IKeyboardListener;
import com.raccoonagents.tools.cascet2d.interfaces.IMouseTarget;
import com.raccoonagents.tools.cascet2d.interfaces.IUpdateable;
import com.raccoonagents.tools.cascet2d.interfaces.rendering.IRenderableObject;
import com.raccoonagents.tools.cascet2d.model.Text;
import com.raccoonagents.tools.cascet2d.model.Vector2f;
import com.raccoonagents.tools.cascet2d.model.events.KeyEvent;
import com.raccoonagents.tools.cascet2d.model.events.KeyEvent.EventType;
import com.raccoonagents.tools.cascet2d.util.CalculationUtils;
import com.raccoonagents.tools.cascet2d.util.FontUtils;
import com.raccoonagents.tools.cascet2d.util.GraphicUtil;
import com.raccoonagents.tools.cascet2d.util.KeyboardEventFactory;
import com.raccoonagents.tools.cascet2d.util.MouseManager;

public class TextField extends AbstractStageUIComponent implements IMouseTarget, IRenderableObject, IKeyboardListener, IUpdateable {
	protected boolean hasFocus = false;
	private boolean selectable;
	protected Text text;	
	protected float [] originalColor = new float []{0.5f,0.5f,0.5f};
	protected float [] color = originalColor;
	public static int [] exitKeys = new int []{Keyboard.KEY_ESCAPE, Keyboard.KEY_RETURN};
	
	public Text getText() {
		return text;
	}

	public void setText(final Text text) {
		this.text = text;
	}

	public boolean isSelectable() {
		return selectable;
	}

	public void setSelectable(final boolean selectable) {
		this.selectable = selectable;
	}

	public TextField(final Vector2f size) {
		super();
		this.size = size;
		KeyboardEventFactory.addKeyboardListener(this);
		
	}

	@Override
	public void render() {
		if(!visible){
			return;
		}
		GraphicUtil.renderRectangle(this);
		FontUtils.renderText(localLocation.x, localLocation.y, text.getText(), text.getColor(), 1.0f,text.getOpacity());
	}

	@Override
	public boolean isMouseInside() {
		final Vector2f wc = parent.getWorldCoordinates(this.getLocation());
		return CalculationUtils.isPointInside(wc, getSize(), MouseManager.getMouseCoords());
	}


	

	@Override
	public float[] getColor() {
		return color;
	}

	@Override
	public float getOpacity() {
		return 0.5f;
	}


	@Override
	public float getRotation() {
		return 0;
	}
	
	
	@Override
	public void acceptKeyEvent(final KeyEvent e) {
		// Keyboard.KEY_LSHIFT
		if(hasFocus && e.getType() == EventType.TYPED){			
			
			for(final int k : exitKeys){
				if(e.getKey() == k){
					notifyListeners("value was entered.");
					hasFocus = false;
					return;
				}
			}
			
			if(e.getKey() == Keyboard.KEY_BACK || e.getKey() == Keyboard.KEY_DELETE){
				String txt = text.getText();
				if(txt.length() > 0){
					txt = txt.substring(0, txt.length() -1);
					text.setText(txt);
				}		
			}else{
				text.append(""+e.getCharValue());			
				
			}
		}		
	}

	@Override
	public void update() {
		color = originalColor;				
		if(isMouseInside()){
			if(Mouse.isButtonDown(Mouse.getEventButton())){
				hasFocus = true;
			}else{
				color = new float []{1.0f,0.0f,0.0f};
			}
		}else{
			if(Mouse.isButtonDown(Mouse.getEventButton())){
				notifyListeners("value was entered.");
				hasFocus = false;
			}
		}

		if(hasFocus){
			color = new float []{0.3f,0.0f,0.0f};	
		}		
	}

}
